package org.com.lcuiot.database.mapper;

import io.mybatis.mapper.Mapper;
import org.apache.ibatis.annotations.Param;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.com.lcuiot.database.entity.DeviceData;

import java.util.List;

/**
 * 设备数据点
 *
 * @author gaoge
 * @since 2022/10/27 14:32
 */
@org.apache.ibatis.annotations.Mapper
public interface DeviceDataMapper extends Mapper<DeviceData, Long> {

    /**
     * 查询近30天数据(时间倒序取出30条，在正序)
     *
     * @param deviceId 设备ID
     * @return List<DeviceData>
     */
    @Select("select * from (select * from device_data where `dev_id` = #{deviceId} and `ds_id` = #{dsId} order by `data_at` desc limit 30) temp order by data_at asc")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "dev_id", property = "devId"),
            @Result(column = "ds_id", property = "dsId"),
            @Result(column = "data_at", property = "dataAt"),
            @Result(column = "data_value", property = "dataValue"),
            @Result(column = "create_at", property = "createAt"),
            @Result(column = "update_at", property = "updateAt")
    })
    List<DeviceData> selectRecentThirtyData(@Param("deviceId") String deviceId, @Param("dsId") String dsId);

    /**
     * 根据设备ID查询最近一条数据
     *
     * @param deviceId 设备ID
     * @return DeviceData
     */
    @Select("select * from device_data where `dev_id` = #{deviceId} order by `data_at` desc limit 1 ")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "dev_id", property = "devId"),
            @Result(column = "ds_id", property = "dsId"),
            @Result(column = "data_at", property = "dataAt"),
            @Result(column = "data_value", property = "dataValue"),
            @Result(column = "create_at", property = "createAt"),
            @Result(column = "update_at", property = "updateAt")
    })
    DeviceData selectRecentOneByDevId(@Param("deviceId") String deviceId);

    /**
     * 根据设备ID查询最近一条数据
     *
     * @param deviceId 设备ID
     * @return DeviceData
     */
    @Select("select * from device_data where `dev_id` = #{deviceId} and `ds_id` = #{dsId} order by `data_at` desc limit 1 ")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "dev_id", property = "devId"),
            @Result(column = "ds_id", property = "dsId"),
            @Result(column = "data_at", property = "dataAt"),
            @Result(column = "data_value", property = "dataValue"),
            @Result(column = "create_at", property = "createAt"),
            @Result(column = "update_at", property = "updateAt")
    })
    DeviceData selectRecentByDevIdAndDsId(@Param("deviceId") String deviceId, @Param("dsId") String dsId);


}
